using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Hansvits.MyGoogle.Contacts
{
    public partial class LoginWindow : Form
    {
        public LoginWindow()
        {            
            InitializeComponent();
            this.BackColor = ColorTranslator.FromHtml("#d4e4ff");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            loginWorker.CancelAsync();
            this.Close();
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {            
            ChangeMode(false);
            loginWorker.RunWorkerAsync();
        }

        private void ChangeMode(bool state)
        {
            txtUsername.Enabled = state;
            txtPassword.Enabled = state;
            btnLogin.Enabled = state;
            pbProcess.Visible = !state;
        }

        private void DisplayStatus(string message, MessageType type)
        {
            lblStatus.Text = message;
            switch (type)
            {
                case MessageType.Normal:
                    lblStatus.ForeColor = SystemColors.ControlText;
                    break;
                case MessageType.Success:
                    lblStatus.ForeColor = Color.Green;
                    break;
                case MessageType.Warning:
                    lblStatus.ForeColor = Color.Orange;
                    break;
                case MessageType.Error:
                    lblStatus.ForeColor = Color.Red;
                    break;
                default:
                    lblStatus.ForeColor = SystemColors.ControlText;
                    break;
            }
        }

        private void loginWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = "ERROR";
            loginWorker.ReportProgress(10, "authenticating user ...");
            bool result = ContactManager.Authenticate(txtUsername.Text, txtPassword.Text);
            if (result == false)
            {
                loginWorker.ReportProgress(0, "authentication failed, invalid username or password");
            }
            else
            {
                loginWorker.ReportProgress(20, "user authenticated");
                Thread.Sleep(TimeSpan.FromSeconds(2));
                loginWorker.ReportProgress(40, "fetching contacts");
                ContactManager.FetchContacts();
                loginWorker.ReportProgress(60, "all contacts retrieved");
                e.Result = "SUCCESS";
            }            
        }

        private void loginWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            switch (e.ProgressPercentage)
            {
                case 0:
                    DisplayStatus((string)e.UserState, MessageType.Error);
                    break;
                case 10:
                    DisplayStatus((string)e.UserState, MessageType.Normal);
                    break;
                case 20:
                case 40:
                    DisplayStatus((string)e.UserState, MessageType.Success);
                    break;
                default:
                    break;
            }
        }

        private void loginWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            string result = (string)e.Result;
            if (e.Cancelled == true)
                DisplayStatus("user cancelled ...", MessageType.Warning);
            else if (e.Error != null)
                DisplayStatus(string.Format("An error occured : {0}", e.Error.Message), MessageType.Error);
            else
            {
                switch (result)
                {
                    case "SUCCESS":
                        ContactWindow window = new ContactWindow();
                        this.Hide();
                        window.Show();
                        break;
                    case "ERROR":
                        break;
                    default:
                        DisplayStatus("An unknown error occured", MessageType.Error);                        
                        break;
                }
            }
            ChangeMode(true);
        }
    }
}